<!-- HTML header for doxygen 1.8.3.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.3.1"/>
<title>AngelScript: Fine tuning</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="test/javascript" src="touch.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.3.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_finetuning.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Fine tuning </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Here's a few recommendations to get the most performance out of AngelScript.</p>
<h1><a class="anchor" id="doc_finetuning_1"></a>
Cache the functions and types</h1>
<p>Doing searches by function declaration or name is rather time consuming and should not be done more than once per function that will be called. The same goes for the types that you might use it.</p>
<p>Also try to use the actual <a class="el" href="classas_i_script_function.html">asIScriptFunction</a> or <a class="el" href="classas_i_type_info.html">asITypeInfo</a> pointers instead of the ids where possible. This will save the engine from translating the id to the actual object.</p>
<p>You may use the user data in the various engine interfaces to store the cached information. For example, store a structure with the commonly used class methods as <a class="el" href="classas_i_type_info.html#a5e8ea071f1c1f3b7c6dfc1950bec73f4">user data</a> in the <a class="el" href="classas_i_type_info.html">asITypeInfo</a> interface. This way you will have quick access to the functions when they need to be called.</p>
<h1><a class="anchor" id="doc_finetuning_2"></a>
Reuse the context object</h1>
<p>The context object is a heavy weight object and you should avoid allocating new instances for each call. The object has been designed to be reused for multiple calls.</p>
<h2><a class="anchor" id="doc_finetuning_2_1"></a>
Context pool</h2>
<p>Ideally the application will keep a simple memory pool of allocated context objects, where new objects are only allocated if the is no free objects in the pool.</p>
<p>By using the engine's <a class="el" href="classas_i_script_engine.html#ae5ba9fe99b72c60392cdaeef164f2c65">context callbacks</a>, this context pool will also be automatically available to the engine for use in internal calls and the add-ons.</p>
<p>The code that wants to use a context from the pool should use the <a class="el" href="classas_i_script_engine.html#a32391ee83e58083b406ba068ab2ee049">RequestContext</a> and <a class="el" href="classas_i_script_engine.html#a22e42bf32902cbd6885731a6beeaca20">ReturnContext</a> methods instead of the <a class="el" href="classas_i_script_engine.html#a2630e1cd03ffab0fee9b820bf0afe42a">CreateContext</a> method.</p>
<p>Here's a simple implementation of a context pool.</p>
<div class="fragment"><div class="line">std::vector&lt;asIScriptContext *&gt; pool;</div>
<div class="line"><a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *RequestContextCallback(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine, <span class="keywordtype">void</span> *param)</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// Get a context from the pool, or create a new</span></div>
<div class="line">  <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx = 0;</div>
<div class="line">  <span class="keywordflow">if</span>( pool.size() )</div>
<div class="line">  {</div>
<div class="line">    ctx = *pool.rbegin();</div>
<div class="line">    pool.pop_back();</div>
<div class="line">  }</div>
<div class="line">  <span class="keywordflow">else</span></div>
<div class="line">    ctx = engine-&gt;<a class="code" href="classas_i_script_engine.html#a2630e1cd03ffab0fee9b820bf0afe42a" title="Creates a new script context.">CreateContext</a>();</div>
<div class="line"></div>
<div class="line">  <span class="keywordflow">return</span> ctx;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> ReturnContextToPool(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine, <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx, <span class="keywordtype">void</span> *param)</div>
<div class="line">{</div>
<div class="line">  pool.push_back(ctx);</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// Unprepare the context to free non-reusable resources</span></div>
<div class="line">  ctx-&gt;<a class="code" href="classas_i_script_context.html#ae3c18a2cc66c56f840e6ee4310287f65" title="Frees resources held by the context.">Unprepare</a>();</div>
<div class="line">}</div>
</div><!-- fragment --><h2><a class="anchor" id="doc_finetuning_2_2"></a>
Nested calls</h2>
<p>Another form of reusing a context is to use nested calls.</p>
<p>Whenever an application registered function that is called from a script needs to execute another script it can reuse the already active context for a nested call. This way it is not necessary to look for an available context from a pool or allocate a new context just for this call.</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> Func()</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// Get the active context</span></div>
<div class="line">  <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx = <a class="code" href="angelscript_8h.html#ad3a20dc58093b92a5a44c7b6ada34a10" title="Returns the currently active context.">asGetActiveContext</a>();</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Store the current context state so we can restore it later</span></div>
<div class="line">  <span class="keywordflow">if</span>( ctx &amp;&amp; ctx-&gt;<a class="code" href="classas_i_script_context.html#ad8f7637a23d67e227d07f65621b6cdd6" title="Backups the current state to prepare the context for a nested call.">PushState</a>() &gt; 0 )</div>
<div class="line">  {</div>
<div class="line">    <span class="comment">// Use the context normally, e.g.</span></div>
<div class="line">    <span class="comment">//  ctx-&gt;Prepare(...);</span></div>
<div class="line">    <span class="comment">//  ctx-&gt;Execute(...);</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">// Once done, restore the previous state</span></div>
<div class="line">    ctx-&gt;<a class="code" href="classas_i_script_context.html#a5d963974625e582799b5d911d182d9be" title="Restores the state to resume previous script execution.">PopState</a>();</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_finetuning_3"></a>
Compile scripts without line cues</h1>
<p>The line cues are normally placed in the bytecode between each script statement. These are where the VM will allow the execution to be suspended, and also where the line callback is invoked.</p>
<p>If you do not need to receive a callback for every statement executed in a script then you may get a little more performance out of the script by compiling without the line cues.</p>
<p>The line callback will still work and is guaranteed to be invoked at least once per loop and every function call in the script to allow the application to interrupt infinite loops or infinitely recursive calls.</p>
<div class="fragment"><div class="line">engine-&gt;<a class="code" href="classas_i_script_engine.html#a1bce4e5f573a2ca0ff55163e28f761dd" title="Dynamically change some engine properties.">SetEngineProperty</a>(<a class="code" href="angelscript_8h.html#a53c2e8a74ade77c928316396394ebe0fa73b396e4ea6376f0962d19add962bd91" title="Remove SUSPEND instructions between each statement. Default: false.">asEP_BUILD_WITHOUT_LINE_CUES</a>, <span class="keyword">true</span>);</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_finetuning_4"></a>
Disable thread safety</h1>
<p>If your application only uses one thread to invoke the script engine, then it may be worth it to compile the library without the thread safety to gain a little more performance.</p>
<p>To do this, define the AS_NO_THREADS flag in the as_config.h header or in the project settings when compiling the library.</p>
<h1><a class="anchor" id="doc_finetuning_5"></a>
Turn off automatic garbage collection</h1>
<p>While garbage collection is important in long running applications, it may be of interest to turn off the automatic garbage collection and then run the garbage collector manually in a controlled manner. The garbage collector is incremental so you shouldn't see long stalls while it is running, but it will consume CPU cycles that may be needed for other things.</p>
<p>To turn off the automatic garbage collector set the engine property <a class="el" href="angelscript_8h.html#a53c2e8a74ade77c928316396394ebe0fa9b5d1d8ff5143a6a77dfd18143d87c7d">asEP_AUTO_GARBAGE_COLLECT</a> to false.</p>
<div class="fragment"><div class="line">engine-&gt;<a class="code" href="classas_i_script_engine.html#a1bce4e5f573a2ca0ff55163e28f761dd" title="Dynamically change some engine properties.">SetEngineProperty</a>(<a class="code" href="angelscript_8h.html#a53c2e8a74ade77c928316396394ebe0fa9b5d1d8ff5143a6a77dfd18143d87c7d" title="Enable or disable automatic garbage collection. Default: true.">asEP_AUTO_GARBAGE_COLLECT</a>, <span class="keyword">false</span>);</div>
</div><!-- fragment --><dl class="section see"><dt>See Also</dt><dd><a class="el" href="doc_gc.html">Garbage collection</a></dd></dl>
<h1><a class="anchor" id="doc_finetuning_6"></a>
Compare native calling convention versus generic calling convention</h1>
<p>If you have specific functions that are called very frequently it may be worth comparing the performance between binding the functions using native calling convention versus the generic calling convention. It is not possible to generalize and say that one is always faster than the other, as it will vary depending on the function signature and the platforms ABI complexity.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="doc_generic.html">The generic calling convention</a>, <a class="el" href="doc_register_func.html#doc_register_func_2">Calling convention</a> </dd></dl>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun Dec 18 2016 12:35:28 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.3.1 </li>
  </ul>
</div>
</body>
</html>
